﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    public class ClientInfoVisitHistoryDAL
    {
        private static readonly Database Database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        /// <summary>
        /// 新增浏览记录
        /// </summary>
        /// <param name="empolyeeID"></param>
        /// <param name="clientID"></param>
        public void AddVisitHistory(int empolyeeID, string clientID)
        {
            const string sql = "insert into ClientInfoVisitHistory (empolyeeID,clientID,visitTime) values (@empolyeeID,@clientID,getdate())";

            using (var cmd = Database.GetSqlStringCommand(sql))
            {
                Database.AddInParameter(cmd, "(@empolyeeID", DbType.Int32, empolyeeID);
                Database.AddInParameter(cmd, "@clientID", DbType.String, clientID);

                Database.ExecuteNonQuery(cmd);
            }
        }

        /// <summary>
        /// 查看某人浏览记录
        /// </summary>
        /// <param name="empolyeeID"></param>
        /// <param name="clientID"></param>
        /// <returns></returns>
        public int GetCountOfVisitHistory(int empolyeeID, string clientID)
        {
            const string sql = "select count(*) from ClientInfoVisitHistory where empolyeeID=@empolyeeID and clientID=@clientID)";

            using (var cmd = Database.GetSqlStringCommand(sql))
            {
                Database.AddInParameter(cmd, "(@empolyeeID", DbType.Int32, empolyeeID);
                Database.AddInParameter(cmd, "@clientID", DbType.String, clientID);

                return Convert.ToInt32(Database.ExecuteScalar(cmd));
            }
        }
    }
}
